<template>
<!--  <router-link :to="'/goodsDetail/' + data.goods_id" class="card" :class="{'gridCard': !type}">-->
    <div class="card" :class="{'gridCard': !type}" @click="$router.push({path: '/goodsDetail/' + data.goods_id})">
      <div class="left">
        <img class="goodsImage" :src="data.goods_img" alt="" v-imgField />
      </div>
      <div class="right">
        <p class="goodsName"><i class="self"
                                v-if="data.is_self">自营</i>{{ data.goods_name }}</p>
        <p class="goodsPrice"
           v-if="!data.goods_marketprice==0">
          <em>￥</em>{{ +data.goods_marketprice }}
          <em>会员价：{{ +data.goods_saleprice }}</em>
        </p>
        <p class="goodsPrice"
           v-if="data.goods_marketprice==0">
          <em>￥</em>{{ data.goods_saleprice.toFixed(2) }}
        </p>
        <p class="goodsTag">
          <span>好评率 {{data.goods_positive || 0}}% 已售出 {{ data.goods_salenum.toFixed(0) || 0}}</span>
        </p>
        <p class="goodsSeller">
          <img class="sellerLogo"
               v-lazy="data.seller_logo"
               alt="" />
          <span class="sellerName">
          {{ data.shop_name }}
        </span>
          <span class="sellerDistance"
                v-if="data.seller_distance && data.seller_distance <6000 && data.seller_distance>0">
          {{ data.seller_distance.toFixed(1) }}km
        </span>
        </p>
        <button class="goodsShop" @click.stop="joinShoppingCart()"><img src="../../../assets/img/icon_shop.png" alt=""></button>
      </div>
    </div>
<!--  </router-link>-->
</template>

<script>
import {addShoppingCart} from '@api'
import qs from 'qs'
import { SET_LOADING } from '@store/type'
import {mapMutations} from 'vuex'

export default {
  props: {
    data: {},
    type: true
  },
  methods: {
    ...mapMutations({
      setLoading: SET_LOADING
    }),
    // 验证购买条件
    // isCanBuy () {
    //   let goods = this.data
    //
    //   // 多规格商品验证商品规格选择
    //   if (goods.is_enablespec === 1) {
    //     // 多规格商品
    //     if (!goods.goods_sku) {
    //       this.$vux.alert.show({
    //         title: '提示',
    //         content: '请先选择商品规格!'
    //       })
    //       return false
    //     }
    //   }
    //
    //   // 商品验证购买数量，服务不限制
    //   if (goods.goods_saletype === 1) {
    //     if (goods.goods_buynum > goods.goods_storage) {
    //       this.$toastText('当前商品库存不足' + goods.goods_buynum + '个！')
    //       return false
    //     }
    //   } else {
    //     if (goods.goods_buynum > goods.goods_storage) {
    //       this.$toastText('当前服务每次最多仅能购买' + goods.goods_storage + '次！')
    //       return false
    //     }
    //   }
    //
    //   return true
    // },
    // 加入购物车
    async joinShoppingCart () {
      // if (!this.isCanBuy()) {
      //   this.$refs.specPanel.show()
      //   return
      // }

      this.setLoading({
        status: true,
        opacity: 0
      })
      let goods = this.data
      let valueids = null
      let valuenames = null
      let skuid = null
      if (goods.is_enablespec === 1 && goods.goods_sku) {
        skuid = goods.goods_sku.sku_id
        valueids = goods.goods_sku.sku_valueids
        valuenames = goods.goods_sku.sku_valuenames
      }
      let shoppingCarGoods = {
        seller_id: goods.seller_id,
        goods_id: goods.goods_id,
        goods_name: goods.goods_name,
        sku_id: skuid,
        sku_valueids: valueids,
        sku_valuenames: valuenames,
        goods_num: goods.goods_buynum,
        goods_saleprice: goods.goods_saleprice,
        goods_saletype: goods.goods_saletype,
        shipping_method: 0,
        is_appointment: goods.is_enableappoint
      }
      let res = await addShoppingCart(qs.stringify(shoppingCarGoods))
      this.setLoading(false)
      if (res && res.code === 200) {
        this.$toastText(res.msg)
        // this.$refs.specPanel.hide()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import "../../../assets/css/pub";

.card {
  padding: .25rem;
  display: flex;
  background-color: #fff;

  .left {
    width: 3rem;
    height: 3rem;
    flex: none;

    .goodsImage {
      width: 100%;
      height: 100%;
      padding: 0;
    }
  }
  .right {
    padding-left: .25rem;
    // width: 100%;
    width: calc(100% - 3rem);
    height: 3rem;
    position: relative;
    // 商品名称
    .goodsName {
      height: 1rem;
      line-height: .53rem;
      font-size: $default_font_size;
      color: $default_title_color;
      vertical-align: top;
      @include textHideByLine(2);
      i.self {
        font-style: normal;
        margin-right: .125rem;
        padding: 0 .125rem;
        background-color: $default_theme_color;
        color: #fff;
        font-size: $default_smallfont_size;
      }
    }
    // 商品价格
    .goodsPrice {
      margin-top: .25rem;
      /*height: .55rem;*/
      font-size: $default_bigfont_size;
      color: $default_price_color;
      overflow: visible;
      em {
        font-size: $default_smallfont_size;
        font-style: normal;
      }
      .reduce {
        border: .025rem solid $default_red_color;
        color: $default_red_color;
        padding: 0 .125rem;
        border-radius: .2rem;
        font-size: $default_middlefont_size;
      }
    }
    // 商品标签
    .goodsTag {
      margin-top: .2rem;
      height: .4rem;
      font-size: $default_middlefont_size;
      color: $default_subtitle_color;
      overflow: visible;
    }
    // 商品店铺
    .goodsSeller {
      margin-top: .2rem;
      height: .4rem;
      font-size: $default_middlefont_size;
      display: flex;
      align-items: center;
      .sellerName {
        flex: 0 1 calc(100% - 1.65rem);
        line-height: .4rem;
        @include textHideByLine(1);
      }
      .sellerLogo {
        width: .4rem;
        height: .4rem;
        border-radius: .125rem;
        margin-right: .125rem;
      }
      .sellerDistance {
        width: 1.5rem;
        text-align: right;
      }
    }
    // 购物按钮
    .goodsShop{
      position: absolute;
      right: 0;
      bottom: 0;
      width: 1rem;
      height: 1rem;
      background: #fff;
      border: 1px solid #fd6a6a;
      border-radius: 500px;
      color: #fd6a6a;
      padding: .15rem;
      img{
        width: 100%;
        height: 100%;
      }
    }
  }
}

.gridCard {
  display: inline-block;
  width: 4.87rem;
  height: 7.75rem;
  padding: .25rem;
  box-sizing: border-box;
  flex-direction: column;
  .left {
    width: 4.0625rem;
    height: 4.0625rem;
  }
  .right {
    width: 4.0625rem;
    padding-left: 0;
    padding-top: .25rem;
  }
  &:nth-child(2n) {
    margin-left: .25rem;
  }
}
</style>
